import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @program: javatopdf
 * @packageName: PACKAGE_NAME
 * @className DirectoryToPdf
 * @description: 将目录下的所有内容转化为层次分明的pdf文件，方便手机阅读和学习
 * @author: xj
 * @create: 2021-01-25 20:13:57
 **/
public class DirectoryToTxt {

    /*
     * 技术方案：树的深度遍历，从上到下打印。
     * 使用的数据结构：栈，使用递归算法
     * */
    public static void main(String[] args) {
        List<String> txtContext = new ArrayList<>();

        String path = "C:\\hydee\\workspace\\study-knowledgeBase";
        File root = new File(path);
        List<Node> fileNames = new ArrayList<>();
        findFileList(root, fileNames, 0);
        for (Node node : fileNames) {
            String prefix = "";
            int high = node.getHigh();
            for (int i = 0; i < high; i++) {
                prefix += "\t";
            }
            String fileName = prefix + node.getFileName();
            System.out.println(fileName);
            txtContext.add(fileName);

            if (node.getType() == 2) {
                List<String> context = node.getContext();
                for (String value : context) {
                    String fileLine = prefix + "\t" + value;
                    System.out.println(fileLine);
                    txtContext.add(fileLine);
                }
            }
        }

        //将List转化为txt文件,因为pdf的内容格式不好看，而且暂不支持中文布局，暂定使用txt使用
        createListStringFile(txtContext, "dest.txt");
    }

    /*
     * 获取路径下的所有文件，包括子目录
     * */
    public static void findFileList(File dir, List<Node> fileNames, int high) {
        if (!dir.exists() || !dir.isDirectory()) {
            return;
        }
        String[] files = dir.list();
        for (int i = 0; i < files.length; i++) {
            File file = new File(dir, files[i]);

            Node node = new Node();
            node.setHigh(high)
                    .setFileName(file.getName());

            /*
            * 过滤指定目录和文件
            * */
            if(file.getName().contains(".idea")
                    || file.getName().contains("target")
                    || file.getName().contains(".iml")
                    || file.getName().contains(".git")
            ){
                continue;
            }

            if (file.isFile()) {
                /*
                 * 只保留指定文件
                 * */
                if (file.getName().contains(".java")
                        || file.getName().contains(".txt")
                        || file.getName().contains(".yml")
                        || file.getName().contains(".xml")
                        || file.getName().contains(".properties")
                        || file.getName().contains(".sql")
                        || file.getName().contains(".html")
                        || file.getName().contains(".js")
                        || file.getName().contains(".json")
                        || file.getName().contains(".http")
                        || file.getName().contains(".lua")
                        || file.getName().contains(".go")
                ) {
                    List<String> context = readFile(file);
                    node.setType(2).setContext(context);
                    fileNames.add(node);
                }
            } else {
                node.setType(1).setContext(null);
                fileNames.add(node);
            }


            if (file.isDirectory()) {
                findFileList(file, fileNames, high + 1);
            }
        }
    }


    /**
     * 读取文件内容
     */
    public static List<String> readFile(File file) {
        List<String> list = new ArrayList<>();
        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    /*
     * 将list<String>创建txt文件
     * */
    public static File createListStringFile(List<String> list, String fileName) {
        File file = null;
        BufferedWriter bw = null;
        BufferedWriter bwclear;

        try {
            file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }

            /*
             * 清空源文件内容
             * */
            bwclear = new BufferedWriter(new FileWriter(file, false)); // 覆盖
            bwclear.write("");
            bwclear.flush();
            bwclear.close();

            /*
             * 开始追加内容
             * */
            bw = new BufferedWriter(new FileWriter(file, true)); // 附加
            for (String line : list) {
                bw.write(line);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }
}

class Node {
    String fileName;
    int high;
    int type; /*1=表示目录，2=表示文件*/
    List<String> context; /*文件内容*/

    public String getFileName() {
        return fileName;
    }

    public Node setFileName(String fileName) {
        this.fileName = fileName;
        return this;
    }

    public int getHigh() {
        return high;
    }

    public Node setHigh(int high) {
        this.high = high;
        return this;
    }

    public int getType() {
        return type;
    }

    public Node setType(int type) {
        this.type = type;
        return this;
    }

    public List<String> getContext() {
        return context;
    }

    public Node setContext(List<String> context) {
        this.context = context;
        return this;
    }
}
